package com.example.testplanetorbit

import android.graphics.*
import kotlin.math.abs

/**
 * 轨道上的小圆点
 */
class OvalDot : BaseDot() {

    var mPathMeasure1: PathMeasure? = null

    var ovalR1 = 8.0f   //小球半径

    var dotPaint: Paint? = null

    var ovalPath1 : Path? = null
//    var mOrbitPathLength : Float = 0f

    var reverseDirection = false  //控制运行的方向，顺时针还是逆时针

    override fun setPath(re21 : RectF?, resetPath:Boolean) {
        if (re21 == null) {return}
        if (ovalPath1 == null || resetPath) {
            ovalPath1 = Path()
            if (reverseDirection) {
                ovalPath1?.addOval(re21, Path.Direction.CCW)
            }else {
                ovalPath1?.addOval(re21, Path.Direction.CW)
            }
            mPathMeasure1?.setPath(ovalPath1, false)
        }

        if (mPathMeasure1 == null) {
            mPathMeasure1 = PathMeasure(ovalPath1, false)
            mOrbitPathLength = mPathMeasure1?.length ?: 0f
            mStep1 = mOrbitPathLength * 1.0f / OrbitData.INVALIDATE_TIMES1
        }

        if (resetPath || mOrbitPathLength <= 0) {
            mOrbitPathLength = mPathMeasure1?.length ?: 0f
            mStep1 = mOrbitPathLength * 1.0f / OrbitData.INVALIDATE_TIMES1
        }
    }

   override fun changeHorizontal(moveLeft:Boolean) {
        if (reverseDirection) {
            if (moveLeft) {
                mDistance1 -= (mOrbitPathLength * 0.01).toInt()
            } else {
                mDistance1 += (mOrbitPathLength * 0.01).toInt()
            }
        } else {
            if (moveLeft) {
                mDistance1 += (mOrbitPathLength * 0.01).toInt()
            } else {
                mDistance1 -= (mOrbitPathLength * 0.01).toInt()
            }
        }
    }

   override fun init(direction: Boolean) {
        if (dotPaint == null) {
            dotPaint = Paint()
            dotPaint?.isAntiAlias = true
            dotPaint?.color = Color.WHITE
            dotPaint?.style = Paint.Style.FILL
            dotPaint?.strokeWidth = 1f
        }
        this.reverseDirection = direction
    }

   override fun drawOval1(canvas: Canvas?) {
       super.drawOval1(canvas)
//        if (firstDrawOval) {
//            mDistance1 = mOrbitPathLength * mInitDistanceRadio
//            firstDrawOval = false
//        }

        while (mDistance1 >= mOrbitPathLength) {
            mDistance1 = mDistance1 - mOrbitPathLength
        }
        while (mDistance1 < 0) {
            mDistance1 = mOrbitPathLength + mDistance1
        }

        mPathMeasure1?.getPosTan(mDistance1, mPos, mTan)

       var radio = 1f
       if (!reverseDirection) {
            ovalR1 = if (mDistance1 > mOrbitPathLength / 2) {
                radio = abs(mDistance1 - 3 * mOrbitPathLength / 4) / (mOrbitPathLength / 4)
                if (radio < 0.75f) {
                    radio = 0.75f
                }
                OrbitData.OVAL_R1 * radio
            } else {
                OrbitData.OVAL_R1
            }
        } else {
            ovalR1 = if (mDistance1 < mOrbitPathLength / 2) {
                radio = abs(mDistance1 - 1 * mOrbitPathLength / 4) / (mOrbitPathLength / 4)
                if (radio < 0.75f) {
                    radio = 0.75f
                }
                OrbitData.OVAL_R1 * radio
            } else {
                OrbitData.OVAL_R1
            }
        }

       dotPaint?.alpha  = (radio * OrbitData.ALPHA_MIN).toInt()

       val rLeft = mPos[0] - ovalR1
        val rRight = mPos[0] + ovalR1
        val rTop = mPos[1] - ovalR1
        val rBottom = mPos[1] + ovalR1
        canvas?.drawOval(rLeft, rTop, rRight, rBottom, dotPaint!!)
        if (!lock) {
            mDistance1 += mStep1
        }
    }
}